/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author sorasaks
 */
public class TextFileReaderUtil {
    
    public static Long readLine(String path) {
        Long line = 0L;
        BufferedReader br = null;

        if (!new File(path).exists()) {
            return line;
        }

        try {
            String sCurrentLine;

            br = new BufferedReader(new FileReader(path));
            
            while ((sCurrentLine = br.readLine()) != null) {
                if (!sCurrentLine.trim().isEmpty()) {
                    line++;
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            return line;
        }
    }

    public static List<String> read(String path) {
        List<String> output = new ArrayList<String>();
        BufferedReader br = null;

        if (!new File(path).exists()) {
            return output;
        }

        try {
            String sCurrentLine;

            br = new BufferedReader(new FileReader(path));

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
                if (!sCurrentLine.trim().isEmpty()) {
                    output.add(sCurrentLine);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            return output;
        }
    }
}
